<template>
  <div class="page-box">
    <!-- 筛选区域 -->
    <div v-show="flag" class="search-box">
      <!-- :inline="true" 表单在一行显示 -->
      <el-form size="mini" :inline="true" class="demo-form-inline">
        <el-form-item label="标签名称">
          <el-input v-model="params.tagname" placeholder="请输入标签名称" />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="search">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 工具条区域 -->
    <div class="tabbar-box">
      <el-row type="flex" justify="space-between">
        <div>
          <el-button plain type="primary" icon="el-icon-plus" size="mini" @click="getdetail('add')">新增</el-button>
          <el-button plain type="warning" icon="el-icon-download" size="mini" @click="exportExcel">导出</el-button>
        </div>
        <el-dialog ref="tagdialog" :title="dialog" :visible.sync="dialogVisible" width="45%" @close="handleClose">
          <el-form ref="tagform" :model="tagform" label-width="100px" class="demo-dynamic" size="medium">
            <el-form-item prop="tagname" label="标签名称" :rules="{ required: true, message: '请输入标签名称', trigger: 'blur' }">
              <el-col :span="20"><el-input v-model="tagform.tagname" /></el-col>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="handleClose">取 消</el-button>
            <el-button size="small" type="primary" @click="submitForm">确 定</el-button>
          </span>
        </el-dialog>
        <div>
          <el-tooltip class="item" effect="dark" content="关闭搜索区" placement="top">
            <el-button :icon="up" size="small" circle @click="hide" />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="刷新" placement="top">
            <el-button icon="el-icon-refresh" size="small" circle @click="reload" />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="筛选" placement="top-start">
            <el-popover placement="bottom" width="100" trigger="click">
              <el-checkbox-group v-model="checkList">
                <el-checkbox class="fa fa fa-dedent" label="ID" />
                <el-checkbox class="fa fa fa-dedent" label="标签名称" />
              </el-checkbox-group>
              <el-button slot="reference" class="filter" icon="el-icon-menu" size="small" circle />
            </el-popover>
          </el-tooltip>
        </div>
      </el-row>
    </div>
    <!-- 表格区域 -->
    <div class="table-box">
      <el-table id="outTable" ref="myTable" :data="taglist" style="width: 100%" border>
        <el-table-column type="index" label="#" width="50" />
        <el-table-column v-if="checkList.includes('ID')" prop="id" label="ID" />
        <el-table-column v-if="checkList.includes('标签名称')" prop="tagname" label="标签名称" />
        <el-table-column prop="" label="操作">
          <template #default="{row}">
            <el-button size="mini" circle type="primary" icon="el-icon-edit" @click="getdetail(row.id)" />
            <el-button size="mini" type="danger" icon="el-icon-delete" circle @click="dodelete(row.id)" />
          </template>
        </el-table-column>

      </el-table>
    </div>
    <!-- 分页区域 -->
    <div class="page">
      <el-pagination
        v-if="isTableAlive"
        background
        :current-page="params.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="params.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
<script>

import { gettags, gettagsdetail, deltags, addtags, edittags } from '@/api/tags'
import { exportTableAsXLSX } from '@/utils/unitools'
export default {
  data() {
    return {
      dialog: '添加标签',
      dialogVisible: false,
      checkList: ['ID', '标签名称'],
      taglist: [],
      tagform: {
        tagname: ''
      },
      params: {
        tagname: '',
        page: 1,
        limit: 10
      },
      flag: true,
      total: 0,
      up: 'el-icon-arrow-up',
      isTableAlive: true

    }
  },
  created() {
    this.gettaglist()
  },
  methods: {
    async getdetail(id) {
      // console.log(id)
      console.log(id)
      if (id === 'add') {
        this.dialog = '添加标签'
        // console.log(1)
        this.dialogVisible = true
      } else {
        this.dialog = '编辑标签'
        const res = await gettagsdetail(id)
        console.log(res)
        this.dialogVisible = true
        this.tagform.tagname = res.tagname
        this.tagform.id = res.id
      }
    },
    dodelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deltags(id)
        console.log(res)
        if (res === '') {
          if (this.taglist.length === 1) {
            this.params.page--
            if (this.params.page < 1) {
              this.params.page = 1
            }
          }
          this.$message.success('删除成功')
          this.gettaglist()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    submitForm() {
      this.$refs.tagform.validate(async(valid) => {
        console.log(this.tagform)
        if (valid) {
          if (this.dialog === '添加标签') {
            const res = await addtags(this.tagform)
            console.log(res)
            if (res === '') {
              this.$message.success('添加成功')
              this.handleClose()
            }
          } else {
            const res = await edittags(this.tagform)
            console.log(res)
            if (res === '') {
              this.$message.success('修改成功')
              this.handleClose()
              this.reload()
            }
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleClose() {
      this.dialogVisible = false
      this.$refs.tagform.resetFields()
    },
    exportExcel() {
      exportTableAsXLSX('outTable')
    },

    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.params.page = 1
      this.params.limit = val
      this.gettaglist()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.gettaglist()
    },

    async gettaglist() {
      const res = await gettags(this.params)
      console.log(res)
      this.taglist = res.data
      this.total = res.count
    },
    hide() {
      this.flag = !this.flag
      this.up = this.flag ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
    },
    reload() {
      this.taglist = []
      this.params.page = 1
      this.isTableAlive = false
      setTimeout(() => {
        this.isTableAlive = true
      }, 100)
      this.gettaglist()
    },
    reset() {
      this.params.tagname = ''
      this.reload()
    },
    search() {
      this.gettaglist()
    }

  }
}
</script>
<style lang="scss" scoped>
.page-box {
  padding: 20px;
  .search-box,
  .tabbar-box {
    padding: 10px 10px;
    height: 50px;
    .filter {
      margin-left: 10px;
    }
  }
  .fontsome {
    margin-left: 15px;
    &:hover {
      border-bottom: 1px solid #51a7ff;
      padding-bottom: 5px;
      color: #51a7ff;
    }
  }
}
</style>
